home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / raytrace / qrt / qrt.lha / qrt / lexer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-03  |  5.5 KB  |  275 lines

  1.  
  2. /**********************************************************
  3.  
  4.       Small lexical analyser for inout. Also contains
  5.       some bounds checking code.
  6.  
  7.  **********************************************************/
  8.  
  9.  
  10. #include "qrt.h"
  11.  
  12. extern int linenumber;
  13.  
  14. # ifdef UNIX
  15. #   include <string.h>
  16. # endif
  17.  
  18. /**********************************************************
  19.  
  20.            Transforms all white spaces to blanks
  21.  
  22.     - changed 13 Mar 89 to handle tabs in input file
  23.  
  24.  **********************************************************/
  25.  
  26. char towhite(c) int c; {
  27.   char c2;
  28.  
  29.   c2 = c;
  30.   if (c2=='\n') linenumber++;
  31.  
  32.   if ((c2=='\n') ||
  33.       (c2=='\t') ||
  34.       (c2=='=')  ||
  35.       (c2==',')  ||
  36.       (c2==';'))     return(' ');
  37.  
  38.   c2 = c;
  39.   return(c2);
  40. }
  41.  
  42.  
  43. /**********************************************************
  44.  
  45.         Removes blank space before next token
  46.  
  47.  **********************************************************/
  48.  
  49. void rmspace() {
  50.   char c;
  51.   while (((c=towhite(fgetc(stdin)))==' ') && !feof(stdin));
  52.   ungetc(c,stdin);
  53. }
  54.  
  55. /**********************************************************
  56.  
  57.  Comment Killer - Added 16 Jun 88 to handle nested comments
  58.  
  59.  **********************************************************/
  60.  
  61. void Comment_Killer()
  62. {
  63.   char c;
  64.  
  65.   c='\0';
  66.   while (c != '}' && !feof(stdin)) {
  67.     c = towhite(fgetc(stdin));
  68.     if (c == '{') Comment_Killer();
  69.   }
  70. }
  71.  
  72. /**********************************************************
  73.  
  74.                Get next token from stdin
  75.  
  76.    - Changed 12 Mar 89 to fix compatibility problem with
  77.      toupper() across different versions of UN*X.
  78.      Aparently some versions change a letter even if it is
  79.      already upper case.
  80.  
  81.  **********************************************************/
  82.  
  83. void GetToken(s)                          /* get a token from stdio */
  84.   char s[];
  85. {                                    /* */
  86.   char c; int x;
  87.  
  88.   x=0; s[0]=c='\0';                       /* char count */
  89.  
  90.   rmspace();
  91.  
  92.   while (!feof(stdin) && x<(SLEN-1)) {
  93.  
  94.     c = towhite(fgetc(stdin));
  95.     if ('a'<=c && c<='z') c=toupper(c);
  96.     s[x++]=c;
  97.  
  98.     if (c==' ') { s[--x]='\0'; break; }
  99.     if (c=='(' || c==')') {
  100.       if (x==1) { s[x]='\0'; break; }
  101.       else {
  102.         ungetc(c,stdin); s[--x]='\0'; break;
  103.       }
  104.     }
  105.     if (c=='{') {
  106.       x--;
  107.       Comment_Killer();
  108.       rmspace();
  109.     }
  110.   }
  111. }
  112.  
  113.  
  114. /**********************************************************
  115.  
  116.       Return value if color is in range 0<=cnum<=CNUM
  117.       otherwise call error routine.
  118.  
  119.  **********************************************************/
  120.  
  121. float InRange(cnum)
  122.   float cnum;
  123. {
  124.   if (cnum>=0.0 && cnum<=1.00) return(cnum);
  125.   Error(COLOR_VALUE_ERR,1501);
  126.  
  127.   return(0);   /* this is to keep lint happy! */
  128. }
  129.  
  130.  
  131. /**********************************************************
  132.  
  133.          Return value if value is >=0.
  134.          otherwise call error routine.
  135.  
  136.  **********************************************************/
  137.  
  138. float IsPos(val)
  139.   float val;
  140. {
  141.   if (val >= 0.0) return(val);
  142.   Error(LESS_THAN_ZERO,1502);
  143.  
  144.   return(0);   /* this is to keep lint happy! */
  145. }
  146.  
  147. /**********************************************************
  148.  
  149.     Reads next number and converts to float from string
  150.  
  151.  **********************************************************/
  152.  
  153. float Get_Next_Num() {
  154.   char str[SLEN];
  155.   float val;
  156.  
  157.   GetToken(str);
  158.  
  159.   val=atof(str);
  160.  
  161. # ifdef IODEBUG
  162.     printf("GETNEXTNUM: token=%s, val=%f\n",str,val);
  163. # endif
  164.  
  165.   return(val);
  166. }
  167.  
  168.  
  169. /**********************************************************
  170.  
  171.       Reads a name from input, and allocates some space
  172.       for it. Returns a pointer to space.
  173.  
  174.  **********************************************************/
  175.  
  176. char *Get_Next_Name() {
  177.   char str[SLEN], *s;
  178.  
  179.   GetToken(str);
  180.  
  181.   if ((s=malloc(strlen(str)+1))==NULL)
  182.     Error(MALLOC_FAILURE,1503);
  183.  
  184.   strcpy(s,str);
  185.  
  186. # ifdef IODEBUG
  187.     printf("GETNEXTNAME: token=%s\n",str);
  188. # endif
  189.  
  190.   return(s);
  191. }
  192.  
  193. /**********************************************************
  194.  
  195.      Reads a number 0..1 and returns a color value
  196.      0..CNUM;
  197.  
  198.  **********************************************************/
  199.  
  200. short Get_Color_Val() {
  201.   return((short)(InRange(Get_Next_Num())*(float)CNUM));
  202. }
  203.  
  204.  
  205. /**********************************************************
  206.  
  207.        Returns true if the next token is a left paren
  208.  
  209.  **********************************************************/
  210.  
  211. void GetLeftParen() {
  212.   char str[SLEN];
  213.  
  214.   GetToken(str);
  215.   if (strcmp(str,"(")!=0) Error(LPAREN_EXPECTED,1504);
  216.   return;
  217. }
  218.  
  219.  
  220. /**********************************************************
  221.  
  222.        Returns true if the next token is a left paren
  223.  
  224.  **********************************************************/
  225.  
  226. int GetRightParen() {
  227.   char str[SLEN];
  228.  
  229.   GetToken(str);
  230.   if (strcmp(str,")")!=0) return(FALSE);
  231.   return(TRUE);
  232. }
  233.  
  234.  
  235. /**********************************************************
  236.  
  237.            Gets a VECTOR structure of the form
  238.            (num1, num2, num3)
  239.  
  240.  **********************************************************/
  241.  
  242. void GetVector(vector)
  243.   VECT_PTR vector;
  244. {
  245.   GetLeftParen();
  246.  
  247.   vector->x = Get_Next_Num();
  248.   vector->y = Get_Next_Num();
  249.   vector->z = Get_Next_Num();
  250.  
  251.   if(!GetRightParen()) Error(ILLEGAL_VECTOR,1505);
  252. }
  253.  
  254.  
  255. /**********************************************************
  256.  
  257.            Gets a SVECTOR structure of the form
  258.            (num1, num2, num3)
  259.  
  260.  **********************************************************/
  261.  
  262. void GetSVector(svector)
  263.   SVECT_PTR svector;
  264. {
  265.   GetLeftParen();
  266.  
  267.   svector->r = Get_Color_Val();
  268.   svector->g = Get_Color_Val();
  269.   svector->b = Get_Color_Val();
  270.  
  271.   if(!GetRightParen()) Error(ILLEGAL_SVECTOR,1506);
  272. }
  273.  
  274.  
  275.